<?php

namespace app\controller\Api\V1\Client;

use app\helper\Config;
use app\helper\GetProductInfo;
use app\model\Channel;
use app\model\GameRechargeConfig;
use app\request\Recharge;
use Exception;
use support\Request;
use support\Response;

class RechargeController
{

    protected Config $configRe;


    public function __construct()
    {
        $this->configRe = Config::getInstance();
    }


    /**
     * 获取充值配置
     *
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function Config(Request $request): Response
    {

        $validate = new Recharge();

        if ($request->input('release_type') != 1) {
            throw new Exception('工具服测试支付', 500);
        }

        if (!$validate->scene('config')->check($request->all())) {
            throw new Exception($validate->getError(), 500);
        }

        // 渠道验证
        $channelVerify = Channel::query()->where('number', $request->input('payment_channel'))
            ->where('state', 1)->exists();
        if (!$channelVerify) {
            throw new Exception('渠道不存在', 500);
        }

        $channelPaymentArr = [
            '10000' => [  // 官方
                'alipay',
                'wechat',
                'sumapay'
            ],
            '10002' => [  // 游码 http://wiki.zjumarkgame.com/sdk/web/#/7/150
                'umark'
            ],
            '10003' => [  // 创奇 https://www.quicksdk.com/doc-15.html
                'quick'
            ],
            '10004' => [  // 合肥协萧
                'xiexiao'
            ],
            '10005' => [  // 栩腾
                'xuteng'
            ]
        ];

        // 获取游戏充值配置
        $rechargeConfigInfo = GameRechargeConfig::query()->where('merchant', $request->input('mch_id'))
            ->where('game', $request->input('game_id'))->first();
        if (!$rechargeConfigInfo) {
            $currency = [];
        } else {
            $rechargeConfigInfo = $rechargeConfigInfo->toArray();

            $currencyConfig = json_decode($rechargeConfigInfo['currency_config'], true);
            $currency = [];
            foreach ($currencyConfig as $item) {
                $currency[] = [
                    'id' => $item['id'],
                    'name' => $item['name'],
                    'ratio' => $item['ratio'],
                ];
            }
        }

        $getProductPlatform = GetProductInfo::getInstance();

        $payPlatformInfo = $getProductPlatform->GetPayPlatform();
        if (empty($payPlatformInfo['api_url'])) {
            throw new Exception('未配置支付平台', 500);
        }

        $data = [
            'min_amount' => $rechargeConfigInfo['min_amount'] ?? null,
            'max_amount' => $rechargeConfigInfo['max_amount'] ?? null,
            'currency' => $currency,
            'payment_api_url' => $payPlatformInfo['api_url'],
            'payment_method' => $channelPaymentArr[$request->input('payment_channel')],
        ];

        return jsonRe($data);

    }

}
